home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Programming / fpc / inc / sstrings.inc < prev    next >
Text File  |  1998-09-21  |  15KB  |  787 lines

  1. {
  2.     $Id: sstrings.inc,v 1.11 1998/08/11 21:39:07 peter Exp $
  3.     This file is part of the Free Pascal run time library.
  4.     Copyright (c) 1993,97 by the Free Pascal development team
  5.  
  6.     See the file COPYING.FPC, included in this distribution,
  7.     for details about the copyright.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12.  
  13.  **********************************************************************}
  14.  
  15. {****************************************************************************
  16.                     subroutines for string handling
  17. ****************************************************************************}
  18.  
  19. {$I real2str.inc}
  20.  
  21. function copy(const s : string;index : StrLenInt;count : StrLenInt): string;
  22.  
  23. begin
  24.   if count<0 then
  25.    count:=0;
  26.   if index>1 then
  27.    dec(index)
  28.   else
  29.    index:=0;
  30.   if index>length(s) then
  31.    count:=0
  32.   else
  33.    if index+count>length(s) then
  34.     count:=length(s)-index;
  35.   Copy[0]:=chr(Count);
  36.   Move(s[Index+1],Copy[1],Count);
  37. end;
  38.  
  39. procedure delete(var s : string;index : StrLenInt;count : StrLenInt);
  40.  
  41. begin
  42.   if index<=0 then
  43.     begin
  44.     count:=count+index-1;
  45.     index:=1;
  46.     end;
  47.   if (Index<=Length(s)) and (Count>0) then
  48.     begin
  49.     if Count+Index>length(s) then
  50.       Count:=length(s)-Index+1;
  51.     s[0]:=Chr(length(s)-Count);
  52.     if Index<=Length(s) then
  53.       Move(s[Index+Count],s[Index],Length(s)-Index+1);
  54.    end;
  55. end;
  56.  
  57. procedure insert(const source : string;var s : string;index : StrLenInt);
  58.  
  59. begin
  60.   if index>1 then
  61.    dec(index)
  62.   else
  63.    index:=0;
  64.   s:=Copy(s,1,Index)+source+Copy(s,Index+1,length(s));
  65. end;
  66.  
  67. function pos(const substr : string;const s : string): byte;
  68.  
  69. var i,j : longint;
  70.     e : boolean;
  71.  
  72. begin
  73.    i := 0;
  74.    j := 0;
  75.    e:=(length(SubStr)>0);
  76.    while e and (i<=Length(s)-Length(SubStr)) do
  77.     begin
  78.       inc(i);
  79.       if (SubStr[1]=s[i]) and (Substr=Copy(s,i,Length(SubStr))) then
  80.        begin
  81.          j:=i;
  82.          e:=false;
  83.        end;
  84.     end;
  85.    Pos:=j;
  86. end;
  87.  
  88. {Faster when looking for a single char...}
  89.  
  90. function pos(c:char;const s:string):byte;
  91.  
  92. var i:longint;
  93.  
  94. begin
  95.     for i:=1 to length(s) do
  96.         if s[i]=c then
  97.             begin
  98.                 pos:=i;
  99.                 exit;
  100.             end;
  101.     pos:=0;
  102. end;
  103.  
  104. {$ifdef IBM_CHAR_SET}
  105. const
  106.   UpCaseTbl : string[7]=#154#142#153#144#128#143#165;
  107.   LoCaseTbl : string[7]=#129#132#148#130#135#134#164;
  108. {$endif}
  109.  
  110. function upcase(c : char) : char;
  111.  
  112. {$IFDEF IBM_CHAR_SET}
  113. var
  114.   i : longint;
  115. {$ENDIF}
  116. begin
  117.   if (c in ['a'..'z']) then
  118.     upcase:=char(byte(c)-32)
  119.   else
  120. {$IFDEF IBM_CHAR_SET}
  121.     begin
  122.       i:=Pos(c,LoCaseTbl);
  123.       if i>0 then
  124.        upcase:=UpCaseTbl[i]
  125.       else
  126.        upcase:=c;
  127.     end;
  128. {$ELSE}
  129.    upcase:=c;
  130. {$ENDIF}
  131.     end;
  132.  
  133. function upcase(const s : string) : string;
  134.  
  135. var i : longint;
  136.  
  137. begin
  138.   upcase[0]:=s[0];
  139.   for i := 1 to length (s) do
  140.     upcase[i] := upcase (s[i]);
  141. end;
  142.  
  143. {$ifndef RTLLITE}
  144.  
  145. function lowercase(c : char) : char;
  146. {$IFDEF IBM_CHAR_SET}
  147. var
  148.   i : longint;
  149. {$ENDIF}
  150. begin
  151.   if (c in ['A'..'Z']) then
  152.    lowercase:=char(byte(c)+32)
  153.   else
  154. {$IFDEF IBM_CHAR_SET}
  155.    begin
  156.      i:=Pos(c,UpCaseTbl);
  157.      if i>0 then
  158.       lowercase:=LoCaseTbl[i]
  159.      else
  160.       lowercase:=c;
  161.    end;
  162.  {$ELSE}
  163.    lowercase:=c;
  164.  {$ENDIF}
  165. end;
  166.  
  167. function lowercase(const s : string) : string;
  168.  
  169. var i : longint;
  170.  
  171. begin
  172.   lowercase [0] := s[0];
  173.   for i := 1 to length (s) do
  174.      lowercase[i] := lowercase (s[i]);
  175. end;
  176.  
  177. function hexstr(val : longint;cnt : byte) : string;
  178.  
  179. const
  180.   HexTbl : array[0..15] of char='0123456789ABCDEF';
  181. var
  182.   i : longint;
  183. begin
  184.   hexstr[0]:=char(cnt);
  185.   for i:=cnt downto 1 do
  186.    begin
  187.      hexstr[i]:=hextbl[val and $f];
  188.      val:=val shr 4;
  189.    end;
  190. end;
  191.  
  192.  
  193.  
  194.  function binstr(val : longint;cnt : byte) : string;
  195.  
  196. var
  197.   i : longint;
  198. begin
  199.   binstr[0]:=char(cnt);
  200.   for i:=cnt downto 1 do
  201.    begin
  202.      binstr[i]:=char(48+val and 1);
  203.      val:=val shr 1;
  204.    end;
  205. end;
  206.  
  207. {$endif RTLLITE}
  208.  
  209.  function space (b : byte): string;
  210.  
  211.  begin
  212.     space[0] := chr(b);
  213.     FillChar (Space[1],b,' ');
  214.  end;
  215.  
  216. {*****************************************************************************
  217.                               Str() Helpers
  218. *****************************************************************************}
  219.  
  220. procedure int_str_real(d : real;len,fr : longint;var s : string);[public, alias : 'STR_REAL'];
  221. begin
  222. {$ifdef i386}
  223.    str_real(len,fr,d,rt_s64real,s);
  224. {$else}
  225.    str_real(len,fr,d,rt_s32real,s);
  226. {$endif}
  227. end;
  228.  
  229. {$ifdef SUPPORT_SINGLE}
  230. procedure int_str_single(d : single;len,fr : longint;var s : string);[public, alias : 'STR_SINGLE'];
  231. begin
  232.    str_real(len,fr,d,rt_s32real,s);
  233. end;
  234. {$endif SUPPORT_SINGLE}
  235.  
  236.  
  237. {$ifdef SUPPORT_EXTENDED}
  238. procedure int_str_extended(d : extended;len,fr : longint;var s : string);[public, alias : 'STR_EXTENDED'];
  239. begin
  240.    str_real(len,fr,d,rt_s80real,s);
  241. end;
  242. {$endif SUPPORT_EXTENDED}
  243.  
  244.  
  245. {$ifdef SUPPORT_COMP}
  246. procedure int_str_comp(d : comp;len,fr : longint;var s : string);[public, alias : 'STR_COMP'];
  247. begin
  248.    str_real(len,fr,d,rt_s64bit,s);
  249. end;
  250. {$endif SUPPORT_COMP}
  251.  
  252.  
  253. {$ifdef SUPPORT_FIXED}
  254. procedure int_str_fixed(d : fixed;len,fr : longint;var s : string);[public, alias : 'STR_FIXED'];
  255. begin
  256.    str_real(len,fr,d,rt_f32bit,s);
  257. end;
  258. {$endif SUPPORT_FIXED}
  259.  
  260.  
  261. procedure int_str_longint(v : longint;len : longint;var s : string);[public, alias : 'STR_LONGINT'];
  262. begin
  263.    int_str(v,s);
  264.    if length(s)<len then
  265.      s:=space(len-length(s))+s;
  266. end;
  267.  
  268.  
  269. procedure int_str_cardinal(v : cardinal;len : longint;var s : string);[public, alias : 'STR_CARDINAL'];
  270. begin
  271.   int_str(v,s);
  272.   if length(s)<len then
  273.     s:=space(len-length(s))+s;
  274. end;
  275.  
  276.  
  277. {*****************************************************************************
  278.                            Val() Functions
  279. *****************************************************************************}
  280.  
  281. Function InitVal(const s:string;var negativ:boolean;var base:byte):Word;
  282. var
  283.   Code : Longint;
  284. begin
  285. {Skip Spaces and Tab}
  286.   code:=1;
  287.   while (code<=length(s)) and (s[code] in [' ',#9]) do
  288.    inc(code);
  289. {Sign}
  290.   negativ:=false;
  291.   case s[code] of
  292.    '-' : begin
  293.            negativ:=true;
  294.            inc(code);
  295.          end;
  296.    '+' : inc(code);
  297.   end;
  298. {Base}
  299.   base:=10;
  300.   if code<=length(s) then
  301.    begin
  302.      case s[code] of
  303.       '$' : begin
  304.               base:=16;
  305.               repeat
  306.                 inc(code);
  307.               until (code>=length(s)) or (s[code]<>'0');
  308.               if length(s)-code>7 then
  309.                code:=code+8;
  310.             end;
  311.       '%' : begin
  312.               base:=2;
  313.               inc(code);
  314.             end;
  315.      end;
  316.   end;
  317.   InitVal:=code;
  318. end;
  319.  
  320.  
  321. procedure val(const s : string;var l : longint;var code : word);
  322. var
  323.   base,u  : byte;
  324.   negativ : boolean;
  325. begin
  326.   l:=0;
  327.   Code:=InitVal(s,negativ,base);
  328.   if Code>length(s) then
  329.    exit;
  330.   if negativ and (s='-2147483648') then
  331.    begin
  332.      Code:=0;
  333.      l:=$80000000;
  334.      exit;
  335.    end;
  336.   while Code<=Length(s) do
  337.    begin
  338.      u:=ord(s[code]);
  339.      case u of
  340.        48..57 : u:=u-48;
  341.        65..70 : u:=u-55;
  342.       97..104 : u:=u-87;
  343.      else
  344.       u:=16;
  345.      end;
  346.      l:=l*longint(base);
  347.      if (u>=base) or ((base=10) and (2147483647-l<longint(u))) then
  348.       begin
  349.         l:=0;
  350.         exit;
  351.       end;
  352.      l:=l+u;
  353.      inc(code);
  354.    end;
  355.   code := 0;
  356.   if negativ then
  357.    l:=0-l;
  358. end;
  359.  
  360.  
  361. procedure val(const s : string;var l : longint;var code : integer);
  362. begin
  363.   val(s,l,word(code));
  364. end;
  365.  
  366.  
  367. procedure val(const s : string;var l : longint);
  368. var
  369.   code : word;
  370. begin
  371.    val (s,l,code);
  372. end;
  373.  
  374.  
  375. procedure val(const s : string;var b : byte);
  376. var
  377.   l : longint;
  378. begin
  379.   val(s,l);
  380.   b:=l;
  381. end;
  382.  
  383.  
  384. procedure val(const s : string;var b : byte;var code : word);
  385. var
  386.   l : longint;
  387. begin
  388.   val(s,l,code);
  389.   b:=l;
  390. end;
  391.  
  392.  
  393. procedure val(const s : string;var b : byte;var code : Integer);
  394. begin
  395.   val(s,b,word(code));
  396. end;
  397.  
  398.  
  399. procedure val(const s : string;var b : shortint);
  400. var
  401.   l : longint;
  402. begin
  403.   val(s,l);
  404.   b:=l;
  405. end;
  406.  
  407.  
  408. procedure val(const s : string;var b : shortint;var code : word);
  409. var
  410.   l : longint;
  411. begin
  412.   val(s,l,code);
  413.   b:=l;
  414. end;
  415.  
  416.  
  417. procedure val(const s : string;var b : shortint;var code : Integer);
  418. begin
  419.   val(s,b,word(code));
  420. end;
  421.  
  422.  
  423. procedure val(const s : string;var b : word);
  424. var
  425.   l : longint;
  426. begin
  427.   val(s,l);
  428.   b:=l;
  429. end;
  430.  
  431.  
  432. procedure val(const s : string;var b : word;var code : word);
  433. var
  434.   l : longint;
  435. begin
  436.   val(s,l,code);
  437.   b:=l;
  438. end;
  439.  
  440.  
  441. procedure val(const s : string;var b : word;var code : Integer);
  442. begin
  443.   val(s,b,word(code));
  444. end;
  445.  
  446.  
  447. procedure val(const s : string;var b : integer);
  448. var
  449.    l : longint;
  450. begin
  451.    val(s,l);
  452.    b:=l;
  453. end;
  454.  
  455.  
  456. procedure val(const s : string;var b : integer;var code : word);
  457. var
  458.    l : longint;
  459. begin
  460.    val(s,l,code);
  461.    b:=l;
  462. end;
  463.  
  464.  
  465. procedure val(const s : string;var b : integer;var code : Integer);
  466. begin
  467.   val(s,b,word(code));
  468. end;
  469.  
  470. procedure val(const s : string;var d : valreal;var code : word);
  471. var
  472.   hd,
  473.   esign,sign : valreal;
  474.   exponent,i : longint;
  475.   flags      : byte;
  476. begin
  477.   d:=0;
  478.   code:=1;
  479.   exponent:=0;
  480.   esign:=1;
  481.   flags:=0;
  482.   sign:=1;
  483.   while (code<=length(s)) and (s[code] in [' ',#9]) do
  484.    inc(code);
  485.   case s[code] of
  486.    '+' : inc(code);
  487.    '-' : begin
  488.            sign:=-1.0;
  489.            inc(code);
  490.          end;
  491.   end;
  492.   while (Code<=Length(s)) and (s[code] in ['0'..'9']) do
  493.    begin
  494.    { Read integer part }
  495.       flags:=flags or 1;
  496.       d:=d*10;
  497.       d:=d+(ord(s[code])-ord('0'));
  498.       inc(code);
  499.    end;
  500. { Decimal ? }
  501.   if (s[code]='.') and (length(s)>=code) then
  502.    begin
  503.       hd:=0.1;
  504.       inc(code);
  505.       { After dot, a number is required. }
  506.       if not(s[code] in ['0'..'9']) or (length(s)<code) then
  507.         begin
  508.            d:=0.0;
  509.            exit;
  510.         end;
  511.       while (s[code] in ['0'..'9']) and (length(s)>=code) do
  512.         begin
  513.            { Read fractional part. }
  514.            flags:=flags or 2;
  515.            d:=d+hd*(ord(s[code])-ord('0'));
  516.            hd:=hd/10.0;
  517.            inc(code);
  518.         end;
  519.    end;
  520.  { Again, read integer and fractional part}
  521.   if flags=0 then
  522.    begin
  523.       d:=0.0;
  524.       exit;
  525.    end;
  526.  { Exponent ? }
  527.   if (upcase(s[code])='E') and (length(s)>=code) then
  528.    begin
  529.       inc(code);
  530.       if s[code]='+' then
  531.         inc(code)
  532.       else
  533.         if s[code]='-' then
  534.          begin
  535.            esign:=-1;
  536.            inc(code);
  537.          end;
  538.       if not(s[code] in ['0'..'9']) or (length(s)<code) then
  539.         begin
  540.            d:=0.0;
  541.            exit;
  542.         end;
  543.       while (s[code] in ['0'..'9']) and (length(s)>=code) do
  544.         begin
  545.            exponent:=exponent*10;
  546.            exponent:=exponent+ord(s[code])-ord('0');
  547.            inc(code);
  548.         end;
  549.    end;
  550. { Calculate Exponent }
  551.   if esign>0 then
  552.     for i:=1 to exponent do
  553.       d:=d*10
  554.     else
  555.       for i:=1 to exponent do
  556.         d:=d/10;
  557. { Not all characters are read ? }
  558.   if length(s)>=code then
  559.    begin
  560.      d:=0.0;
  561.      exit;
  562.    end;
  563. { evalute sign }
  564.   d:=d*sign;
  565. { success ! }
  566.   code:=0;
  567. end;
  568.  
  569. procedure val(const s : string;var d : valreal;var code : integer);
  570. begin
  571.   val(s,d,word(code));
  572. end;
  573.  
  574.  
  575. procedure val(const s : string;var d : valreal);
  576. var
  577.   code : word;
  578. begin
  579.   val(s,d,code);
  580. end;
  581.  
  582.  
  583. {$ifdef SUPPORT_SINGLE}
  584. procedure val(const s : string;var d : single;var code : word);
  585. var
  586.   e : valreal;
  587. begin
  588.   val(s,e,code);
  589.   d:=e;
  590. end;
  591.  
  592.  
  593. procedure val(const s : string;var d : single;var code : integer);
  594. var
  595.   e : valreal;
  596. begin
  597.   val(s,e,word(code));
  598.   d:=e;
  599. end;
  600.  
  601.  
  602. procedure val(const s : string;var d : single);
  603. var
  604.   code : word;
  605.   e    : double;
  606. begin
  607.   val(s,e,code);
  608.   d:=e;
  609. end;
  610. {$endif SUPPORT_SINGLE}
  611.  
  612. {$ifdef DEFAULT_EXTENDED}
  613.  
  614.   { with extended as default the valreal is extended so for real there need
  615.     to be a new val }
  616.  
  617.   procedure val(const s : string;var d : real;var code : word);
  618.   var
  619.     e : valreal;
  620.   begin
  621.     val(s,e,code);
  622.     d:=e;
  623.   end;
  624.  
  625.  
  626.   procedure val(const s : string;var d : real;var code : integer);
  627.   var
  628.      e : valreal;
  629.   begin
  630.     val(s,e,word(code));
  631.     d:=e;
  632.   end;
  633.  
  634.  
  635.   procedure val(const s : string;var d : real);
  636.   var
  637.     code : word;
  638.     e    : valreal;
  639.   begin
  640.     val(s,e,code);
  641.     d:=e;
  642.   end;
  643.  
  644. {$else DEFAULT_EXTENDED}
  645.  
  646.   { when extended is not the default it could still be supported }
  647.  
  648.   {$ifdef SUPPORT_EXTENDED}
  649.  
  650.   procedure val(const s : string;var d : extended;var code : word);
  651.   var
  652.     e : valreal;
  653.   begin
  654.     val(s,e,code);
  655.     d:=e;
  656.   end;
  657.  
  658.   procedure val(const s : string;var d : extended;var code : integer);
  659.   var
  660.      e : valreal;
  661.   begin
  662.     val(s,e,word(code));
  663.     d:=e;
  664.   end;
  665.  
  666.   procedure val(const s : string;var d : extended);
  667.   var
  668.     code : word;
  669.     e    : valreal;
  670.   begin
  671.     val(s,e,code);
  672.     d:=e;
  673.   end;
  674.  
  675.   {$endif SUPPORT_EXTENDED}
  676.  
  677. {$endif DEFAULT_EXTENDED}
  678.  
  679.  
  680. {$ifdef SUPPORT_COMP}
  681. procedure val(const s : string;var d : comp;var code : word);
  682. var
  683.   e : valreal;
  684. begin
  685.   val(s,e,code);
  686.   d:=comp(e);
  687. end;
  688.  
  689.  
  690. procedure val(const s : string;var d : comp;var code : integer);
  691. var
  692.   e : valreal;
  693. begin
  694.   val(s,e,word(code));
  695.   d:=comp(e);
  696. end;
  697.  
  698.  
  699. procedure val(const s : string;var d : comp);
  700. var
  701.   code : word;
  702.   e    : valreal;
  703. begin
  704.   val(s,e,code);
  705.   d:=comp(e);
  706. end;
  707. {$endif SUPPORT_COMP}
  708.  
  709. procedure val(const s : string;var v : cardinal;var code : word);
  710. var
  711.   negativ : boolean;
  712.   base,u  : byte;
  713. begin
  714.   v:=0;
  715.   code:=InitVal(s,negativ,base);
  716.   if (Code>length(s)) or negativ then
  717.    exit;
  718.   while Code<=Length(s) do
  719.    begin
  720.      u:=ord(s[code]);
  721.      case u of
  722.        48..57 : u:=u-48;
  723.        65..70 : u:=u-55;
  724.       97..104 : u:=u-87;
  725.      else
  726.       u:=16;
  727.      end;
  728.      cardinal(v):=cardinal(v)*cardinal(longint(base));
  729.      if (u>base) or (cardinal($ffffffff)-cardinal(v)>cardinal(longint(u))) then
  730.       begin
  731.         v:=0;
  732.         exit;
  733.       end;
  734.      v:=v+u;
  735.      inc(code);
  736.    end;
  737.   code:=0;
  738. end;
  739.  
  740.  
  741. procedure val(const s : string;var v : cardinal);
  742. var
  743.   code : word;
  744. begin
  745.   val(s,v,code);
  746. end;
  747.  
  748.  
  749. procedure val(const s : string;var v : cardinal;var code : integer);
  750. begin
  751.   val(s,v,word(code));
  752. end;
  753.  
  754. {
  755.   $Log: sstrings.inc,v $
  756.   Revision 1.11  1998/08/11 21:39:07  peter
  757.     * splitted default_extended from support_extended
  758.  
  759.   Revision 1.10  1998/08/08 12:28:13  florian
  760.     * a lot small fixes to the extended data type work
  761.  
  762.   Revision 1.9  1998/07/18 17:14:23  florian
  763.     * strlenint type implemented
  764.  
  765.   Revision 1.8  1998/07/10 11:02:38  peter
  766.     * support_fixed, becuase fixed is not 100% yet for the m68k
  767.  
  768.   Revision 1.7  1998/07/02 12:14:19  carl
  769.     * No SINGLE type for non-intel processors!!
  770.  
  771.   Revision 1.6  1998/06/25 09:44:19  daniel
  772.   + RTLLITE directive to compile minimal RTL.
  773.  
  774.   Revision 1.5  1998/06/04 23:45:59  peter
  775.     * comp,extended are only i386 added support_comp,support_extended
  776.  
  777.   Revision 1.4  1998/05/31 14:14:52  peter
  778.     * removed warnings using comp()
  779.  
  780.   Revision 1.3  1998/05/12 10:42:45  peter
  781.     * moved getopts to inc/, all supported OS's need argc,argv exported
  782.     + strpas, strlen are now exported in the systemunit
  783.     * removed logs
  784.     * removed $ifdef ver_above
  785.  
  786. }
  787.